package org.spring.springboot.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.dao.AreaDao;
import org.spring.springboot.domain.Area;
import org.spring.springboot.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 赵新国 on 07/02/2017.
 */
@Service
public class AreaServiceImpl implements AreaService {

    private static final Logger logger = LoggerFactory.getLogger(AreaServiceImpl.class);

    @Autowired
    private AreaDao areaDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Map<String, Area> find() {
        Map<String, Area> dataMap = new HashMap<String, Area>();
        dataMap = redisTemplate.opsForHash().entries("AREAS");
        if (dataMap == null || dataMap.size() == 0) {
            List<Area> areas = areaDao.find();
            for (Area area : areas) {
                dataMap.put(area.getAreaId(), area);
            }
            redisTemplate.opsForHash().putAll("AREAS", dataMap);
            for (Map.Entry<String, Area> entry : dataMap.entrySet()) {
                if (entry.getValue().getAreaId().equals("330727")) {
                    System.out.println(entry.getValue());
                }
            }
        } else {
            Area area = (Area) redisTemplate.opsForHash().get("AREAS", "370683");
            if (area != null && area.getAreaId().equals("370683")) {
                System.out.println(area);
            }
        }
        return dataMap;
    }

    @Override
    public List<Area> export() {
        List<Area> areas = areaDao.find();
        return areas;
    }

    @Override
    public void export2() {
        List<Area> areas = areaDao.find();
    }

}
